package client

import (
	"gorm.io/gorm"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/schemas/resp"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/model/client"
)

type IRechargeGearTypeService interface {
	List(page request.PageReq, listReq req.RechargeGearTypeListReq) (res response.PageResp, e error)
	Detail(id uint) (res resp.RechargeGearTypeResp, e error)
	Add(addReq req.RechargeGearTypeAddReq) (e error)
	Edit(editReq req.RechargeGearTypeEditReq) (e error)
	All(uint2 uint) (res []resp.RechargeGearTypeResp, e error)
}

// NewRechargeGearTypeService 初始化
func NewRechargeGearTypeService(db *gorm.DB) IRechargeGearTypeService {
	return &rechargeGearTypeService{db: db}
}

// rechargeGearTypeService 充值档位服务实现类
type rechargeGearTypeService struct {
	db *gorm.DB
}

// All 角色所有
func (srv rechargeGearTypeService) All(id uint) (res []resp.RechargeGearTypeResp, e error) {
	var dramas []client.RechargeGearType

	model := srv.db.Model(&client.RechargeGearType{})
	if id != 1 {
		model.Where("is_show = ?", 1)
	}
	err := model.Order("t_id desc").Find(&dramas).Error
	if e = response.CheckErr(err, "All Find err"); e != nil {
		return
	}
	response.Copy(&res, dramas)
	return
}

// List 充值档位列表
func (srv rechargeGearTypeService) List(page request.PageReq, listReq req.RechargeGearTypeListReq) (res response.PageResp, e error) {
	// 分页信息
	limit := page.PageSize
	offset := page.PageSize * (page.PageNo - 1)
	// 查询
	model := srv.db.Model(&client.RechargeGearType{})
	if listReq.Title != "" {
		model = model.Where("title like ?", "%"+listReq.Title+"%")
	}
	if listReq.Status >= 0 {
		model = model.Where("status = ?", listReq.Status)
	}
	if listReq.IsShow >= 0 {
		model = model.Where("is_show = ?", listReq.IsShow)
	}
	// 总数
	var count int64
	err := model.Count(&count).Error
	if e = response.CheckErr(err, "List Count err"); e != nil {
		return
	}
	// 数据
	var objs []client.RechargeGearType
	err = model.Limit(limit).Offset(offset).Order("t_id desc").Find(&objs).Error
	if e = response.CheckErr(err, "List Find err"); e != nil {
		return
	}

	resps := []resp.RechargeGearTypeResp{}
	response.Copy(&resps, objs)
	return response.PageResp{
		PageNo:   page.PageNo,
		PageSize: page.PageSize,
		Count:    count,
		Lists:    resps,
	}, nil
}

// Detail 充值档位详情
func (srv rechargeGearTypeService) Detail(id uint) (res resp.RechargeGearTypeResp, e error) {
	var obj client.RechargeGearType
	err := srv.db.Where("t_id = ?", id).Limit(1).First(&obj).Error
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Detail First err"); e != nil {
		return
	}
	response.Copy(&res, obj)
	return
}

// Add 充值档位新增
// Add 充值档位新增
func (srv rechargeGearTypeService) Add(addReq req.RechargeGearTypeAddReq) (e error) {
	var obj client.RechargeGearType
	response.Copy(&obj, addReq)

	// 开启事务
	tx := srv.db.Begin()

	// 创建充值模板
	err := tx.Create(&obj).Error
	if e = response.CheckErr(err, "Add Create err"); e != nil {
		tx.Rollback()
		return
	}

	// 默认添加充值明细
	if err := srv.createDefaultRechargeGears(tx, obj.TID); err != nil {
		tx.Rollback()
		e = response.CheckErr(err, "创建默认充值明细失败")
		return
	}

	// 提交事务
	if err := tx.Commit().Error; err != nil {
		e = response.CheckErr(err, "事务提交失败")
		return
	}

	return
}

// createDefaultRechargeGears 创建默认充值明细
func (srv rechargeGearTypeService) createDefaultRechargeGears(tx *gorm.DB, tID uint) error {
	// 默认充值明细数据
	defaultGears := []client.RechargeGear{
		{
			TID:         tID,
			Type:        1, // 普通充值
			Title:       "38",
			Price:       3800,
			Buy:         3800,
			Gift:        1500,
			VipDays:     0,
			Sign:        1,
			IsDefault:   1,
			Sort:        2,
			Status:      1,
			RechargeNum: 0,
		},
		{
			TID:         tID,
			Type:        1,
			Title:       "35",
			Price:       3500,
			Buy:         3500,
			Gift:        0,
			VipDays:     0,
			Sign:        0,
			IsDefault:   0,
			Sort:        1,
			Status:      1,
			RechargeNum: 0,
		},
		{
			TID:         tID,
			Type:        1,
			Title:       "68",
			Price:       6800,
			Buy:         6800,
			Gift:        4000,
			VipDays:     0,
			Sign:        0,
			IsDefault:   0,
			Sort:        3,
			Status:      1,
			RechargeNum: 0,
		},
		{
			TID:         tID,
			Type:        1,
			Title:       "88",
			Price:       8800,
			Buy:         8800,
			Gift:        8800,
			VipDays:     0,
			Sign:        0,
			IsDefault:   0,
			Sort:        4,
			Status:      1,
			RechargeNum: 0,
		},
		{
			TID:         tID,
			Type:        1,
			Title:       "158",
			Price:       15800,
			Buy:         15800,
			Gift:        18800,
			VipDays:     0,
			Sign:        0,
			IsDefault:   0,
			Sort:        5,
			Status:      1,
			RechargeNum: 0,
		},
		{
			TID:         tID,
			Type:        2, // 会员充值
			Title:       "年度会员",
			Price:       36500,
			Buy:         1,
			Gift:        1,
			VipDays:     365,
			Sign:        3,
			IsDefault:   0,
			Sort:        6,
			Status:      1,
			RechargeNum: 0,
		},
	}

	// 批量创建充值明细
	if err := tx.CreateInBatches(defaultGears, len(defaultGears)).Error; err != nil {
		return err
	}

	return nil
}

// Edit 充值档位编辑
func (srv rechargeGearTypeService) Edit(editReq req.RechargeGearTypeEditReq) (e error) {
	var obj client.RechargeGearType
	err := srv.db.Where("t_id = ?", editReq.TId).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Edit First err"); e != nil {
		return
	}

	err = srv.db.Model(&client.RechargeGearType{}).Where("t_id", obj.TID).Select("is_show", "status", "title").Updates(client.RechargeGearType{
		IsShow: editReq.IsShow,
		Status: editReq.Status,
		Title:  editReq.Title,
	}).Error
	e = response.CheckErr(err, "Edit Updates err")
	return
}
